\documentclass[a4paper,12pt]{article}

\usepackage[margin=1.7cm]{geometry}
\usepackage{colortbl}
\usepackage{enumitem}
\usepackage{listings}
\usepackage[colorlinks, urlcolor=blue]{hyperref}
\usepackage{tocloft}
\usepackage{tipa}
\usepackage{tikz}

\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,positioning,fit,shapes}

\title{\LaTeX{} Experience}
\author{Chen Rushan\\chenrsster@gmail.com}
\date{2009.08.26 13:52}

\setlength{\parskip}{1.5ex}
\setlength{\parindent}{0pt}
\setlength{\cftbeforesecskip}{0.5ex}

\lstset{
    basicstyle=\ttfamily, columns=flexible, framesep=0pt, framextopmargin=3pt,
    framexbottommargin=3pt, framexleftmargin=-1pt, frame=single,
    aboveskip=0.5cm, belowskip=0.2cm
}

\begin{document}

\maketitle

\begingroup
\hypersetup{linkcolor=blue}
\tableofcontents
\endgroup

\section{Suppressing page number}

    To suppress the page number, providing the following command in the
    preamble.

    \begin{lstlisting}[gobble=3]
    \pagestyle{empty}
    \end{lstlisting}

    To my knowledge, this command doesn't have effect on page containing the
    title, due to the \verb=\maketitle= command, to force this page to also not
    print the page number, you need to add the following command after
    \verb=\maketitle=:

    \begin{lstlisting}[gobble=3]
    \thispagestyle{empty}
    \end{lstlisting}

\section{Labeling a self-created counter}

    This involves using command \verb=\refstepcounter=.

    Suppose you want to present some examples numbered from 1 to N, and at
    some point you refer back to example i, then you need to write your code as
    this:

    \begin{lstlisting}[gobble=3]
    ... ...
    \newcounter{expcnter}
    ... ...
    \refstepcounter{expcnter}
    \label{exampleIwant}
    {\bf \em Example \theexpcnter}
    Here comes example i
    ... ...
    \end{lstlisting}

    The above code shows the way to label the counter created by yourself, and
    you can refer example i as usual: \verb=\ref{exampleIwant}=.

\section{The way I use lstlisting} 
    
    Package {\em listings} is very very useful when you want to present code in
    your article, it gives you so much control on how the code is displayed. To
    get the effect you want, you may need to specify some options, for me, they
    are:

    \begin{lstlisting}[gobble=3]
    \lstset{
        basicstyle=\ttfamily,
        columns=flexible,
        framesep=0pt,
        framextopmargin=3pt,
        framexbottommargin=3pt,
        framexleftmargin=-1pt,
        frame=single,
        aboveskip=0.5cm,
        belowskip=0.2cm
    }
    \end{lstlisting}

    Here is some detailed information about these options:

    \begin{itemize}
        \item \verb!basicstyle=\ttfamily! to set the basic font style of this
            listing, I personally prefer \verb=\ttfamily= most of the time.

        \item \verb!columns=flexible!, this controls the width of an output
            character, the default is \verb=[c]fixed= which may seem too 
            wide(at least for me), change it to \verb=flexible=.

        \item \verb!framesep=0pt, framextopmargin=3pt, framexbottommargin=3pt!
            and\\ \verb!framexleftmargin=-1pt! set the distance between the
            frame border and text inside. Note that setting
            \verb!framexleftmargin=-1pt! is to make the frame even narrower.

        \item \verb!frame=single! set the frame style.

        \item \verb!aboveskip=0.5cm, belowskip=0.2cm! set the additional
            distance between this listing and the text above and below.
    \end{itemize}

    Besides, I also use the following options:

    \begin{itemize}
        \item \verb/gobble=<number>/ to gobble {\tt number} characters at the
            beginning of each line, it's very useful to keep your \LaTeX{} code
            clean.

    \end{itemize}

\section{How to include an image?}

    The simplest way to include an image is by using \verb/\includegraphics/
    command, as:

    \begin{lstlisting}[gobble=3]
    \includegraphics[...]{image_path}
    \end{lstlisting}

    For \verb/\includegraphics/ to work, package \verb/graphicx/ should be
    included.

    Here I describe some useful options with \verb/\includegraphics/:

    \begin{enumerate}[topsep=0pt, leftmargin=3ex]
        \item \textbf{viewport}

            The {\em viewport} option may be useful if you want to include
            only part of an image, the syntax is as:

            \begin{lstlisting}[gobble=3, escapechar=\%]
            \includegraphics[viewport=%{\em llx lly urx ury},clip%]{image}
            \end{lstlisting}

            Above, {\em ll} means {\em lower left}, {\em ur} means {\em upper
            right}, and {\em x} is the vertical direction, {\em y} is the
            horizontal direction, {\em (0, 0)} is at the lower left corner of
            the monitor. (The dimension of a A4 pdf document in pixels is about
            600x850)

            Note that don't forget the {\em clip} at the end, or you won't get
            that part of image.

        \item \textbf{scale}

            Like \verb/scale=0.9/

    \end{enumerate}

\section{Add footnote for table}

    Suppose you want a table with footnote as:

    \begin{table}[h]
        \centering
        \begin{minipage}{0.5\textwidth}
            \centering    
            \begin{tabular}{|p{4cm}|p{4cm}|}
                \hline
                Michael Jordan & 23\footnote{What a great number!} \\
                \hline
            \end{tabular}
        \end{minipage}
    \end{table}

    So how to do it? Use {\em minipage} environment to surround the {\em
    tabular} environment, as:

    \begin{lstlisting}[gobble=3, escapechar=\%]
    \begin{table}[h]
        \centering
        \begin{minipage}{0.5\textwidth}
            \centering
            \begin{tabular}{...}
                %{\em something}%\footnote{...}
            \end{tabular}
        \end{minipage}
    \end{table}
    \end{lstlisting}
    
    It produces two effects:

    \begin{enumerate}[topsep=0pt]
        \item With {\em minipage}, you're able to have footnote in {\em
            table} environment, by default, footnote is disabled in {\em table}
            environment. (of course, there're some other ways to achieve this
            effort).

        \item With {\em minipage}, the footnote will be placed right under the
            {\em tabular}, instead of the bottom of current page.
    \end{enumerate}

\section{How to place figures side by side}

    The answer is use {\em minipage}, as:

    \begin{lstlisting}[gobble=3]
    \begin{figure}[ht]
        \begin{minipage}[b]{0.5\linewidth}
            \centering
            \includegraphics[scale=1]{filename1}
            \caption{default}
            \label{fig:figure1}
        \end{minipage}
        \hspace{0.5cm}
        \begin{minipage}[b]{0.5\linewidth}
            \centering
            \includegraphics[scale=1]{filename2}
            \caption{default}
            \label{fig:figure2}
        \end{minipage}
    \end{figure}
    \end{lstlisting}

    So as you can see, {\em minipage} is a very useful environment, it can
    make you possible to add footnote to table, and to place figures side by
    side. And in fact, it can be used in lots of situations, suppose, you
    centered text in your document by placing \verb/\centering/ at the beginning
    of your document, but at some point, you want one paragraph to be normal,
    then {\em minipage} can be used, just surround that paragraph with {\em
    minipage} environment. As the name implies, it creates a mini page, which
    makes things within it being isolated from outer world.

    \textbf{NOTE:}

    \begin{lstlisting}[gobble=3]
    Don't put any blank line between minipages. Blank line between two minipages has
    the same effect as the one used in normal text, which will cause two minipages
    to show stacked instead of side-by-side.
    \end{lstlisting}

\section{My way to present url}

    First include {\em hyperref} package this way:

    \begin{lstlisting}[gobble=3]
    \usepackage[colorlinks, urlcolor=blue]{hyperref}
    \end{lstlisting}

    Then present url as:

    \begin{lstlisting}[gobble=3, escapechar=\%]
    \href{%{\em url}%}{%{\em url description}%}
    \end{lstlisting}

    Contents in the 2nd curly braces will be the one displayed in the final
    document.

    For email, use:

    \begin{lstlisting}[gobble=3, escapechar=\%]
    \href{mailto:%{\em email address}%}{%{\em email description}%}
    \end{lstlisting}

\section{Floats in \LaTeX}

    Here I list some differences between non-floating object and floating
    object: (I only discuss figure floats and table floats)

    \begin{enumerate}
        \item For non-floating object, it's a part of normal text stream, and
            it will always be placed before everything specified after it in
            the \LaTeX source. But this may not be the case for floating object,
            floating object is a totally separate entity, which is no longer part
            of normal text stream, and can be placed relatively arbitrary,
            depending on whether there's room for this object in current page
            and how its placement argument is specified.

        \item A caption can be added for floating object, which is not the case
            for non-floating object.

        \item Floating object is always numbered, so you can refer to it from
            elsewhere in the text. But note that in order to use \verb/\label/
            and \verb/\ref/, you should use caption, and always place
            \verb/\label/ after \verb/\caption/.
    \end{enumerate}

    Here I present some examples to show how a floating environment can be created:

    \textbf{For figure}:

    \begin{lstlisting}[gobble=3]
    \begin{figure}[h]
        \centering
        \includegraphics[width=40mm]{image}
        \caption{image caption}
        \label{img}
    \end{figure}
    \end{lstlisting}

    The {\em [h]} is called {\em placement specifier}, which gives the author
    some control over how certain floats can be placed. Here {\em h} tells
    \LaTeX{} to try to place the floats at the same point it occurs in the text
    (however, it just tries), there are some other specifiers, but I'm not gonna
    to list here, google to find more.

    Note that centering can be achieved by using command \verb=\centering= in the
    floating environment, so you don't need to create a {\em center}
    environment, which saves you some type.

    \textbf{For table}:

    \begin{lstlisting}[gobble=3]
    \begin{table}[h]
        \centering
        \caption{table caption}
        \label{tbl}
        \begin{tabular}{...}
            ...
        \end{tabular}
    \end{table}
    \end{lstlisting}

    If you want more in-depth info, refer to:

    \href{http://en.wikibooks.org/wiki/LaTeX/Floats,_Figures_and_Captions}
        {http://en.wikibooks.org/wiki/LaTeX/Floats,\_Figures\_and\_Captions}

\section{Issues regarding table of contents}

    \subsection{Formatting}

    The formatting of table of contents can be accomplished by package {\em
    tocloft}.

    Suppose you want to change the space between section entries, you need to
    set length \verb=\cftbeforesecskip= as:
    \begin{lstlisting}[gobble=3]
    \setlength{\cftbeforesecskip}{0.5ex}
    \end{lstlisting}
    
    Note that the space between ToC entries is affected by \verb=\parskip=,
    so you should set the above length after setting \verb=\parskip=.

    \subsection{Link color}

    To change the color of ToC links, use {\em hyperref} package.

    In order to make the change of link color local to table of contents, you
    should set link color in this way:

    \begin{lstlisting}[gobble=3]
    \begingroup
    \hypersetup{linkcolor=blue}
    \tableofcontents
    \endgroup
    \end{lstlisting}

    Note that before you're able to change the link color, you should include
    {\em hyperref} package this way:

    \begin{lstlisting}[gobble=3]
    \usepackage[colorlinks]{hyperref}
    \end{lstlisting}

\section{Issues regarding table}

    \begin{enumerate}[leftmargin=3ex, topsep=0pt]
    \setlength{\leftmargin}{-1cm}

        \item To force a column to have a given width, use \verb=p{width}= in
            column specification.

        \item How to increase the row height of a table?

            \begin{enumerate}[topsep=0pt, leftmargin=4ex]
                \item To increase the row height of just a specific row, use
                    \verb=\rule= command, as \\\verb=\rule{0pt}{0.5cm}=, then
                    the row containing this rule will be 0.5cm height.

                \item To increase the row heights of the whole table, use 
                    command like \\ \verb=\renewcommand{\arraystretch}{1.3}=, 
                    this command is used to change the distance between the 
                    rows of a table, for previous example, the inter-row 
                    spacing is increased by 30\%.
            \end{enumerate}

        \item Be careful when using {\em multicolumn}, consider code:

            \begin{lstlisting}[gobble=11]
            \begin{tabular}{|l|l|}
                \multicolumn{1}{|c|}{...} & \multicolumn{1}{c|}{...}
                ...
            \end{tabular}
            \end{lstlisting}

            Note that the column specification in the second {\em multicolumn}
            doesn't preceded with \verb/'|'/.
    \end{enumerate}

\section{Issues regarding list}

    \begin{enumerate}[leftmargin=3ex, topsep=0pt, parsep=1ex, itemsep=0pt]

        \item Set \verb=\itemsep= will only affect current list, and no 
            descendant lists or lists at the same level will be affected.

        \item It's almost impossible to adjust list space in regular way, you 
            have to resort to some special packages like {\em enumitem}, as is 
            used in this document.
    \end{enumerate}

\section{Commonly-used phonetic symbols}

    There's one package called {\em tipa} which can helps you input phonetic
    symbol with ease. Here I list all commonly-used phonetic symbols, all these
    symbols should be written within \verb=\textipa{}=. (Note that all normal
    symbols like \textipa{a}, \textipa{k} are represented by themselves, so I
    omit them in this table).

    \begin{center}
    \begin{tabular}{|p{2cm}|p{2cm}||p{2cm}|p{2cm}|}
    \hline
    \textipa{\ae} & \verb=\ae= & \textipa{2} & \verb=2= \\
    \hline
    \textipa{D} & \verb=D= & \textipa{@} & \verb=@= \\
    \hline
    \textipa{3} & \verb=3= & \textipa{N} & \verb=N= \\
    \hline
    \textipa{8} & \verb=8= & \textipa{O} & \verb=O= \\
    \hline
    \textipa{S} & \verb=S= & \textipa{T} & \verb=T= \\
    \hline
    \textipa{U} & \verb=U= & \textipa{Z} & \verb=Z= \\
    \hline
    \textipa{"} & \verb="= & \textipa{""} & \verb=""= \\
    \hline
    \textipa{I} & \verb=I= &  & \\
    \hline
    \end{tabular}
    \end{center}

    The \textipa{I} can be found in \textipa{/aI/}.

\section{Using tikz to draw picture}

    See file {\em tikz\_examples.tex} for examples.

    There are some points to note:

    \begin{itemize}
        \item Generally, I'll use the following libraries:

            {\em arrows,decorations.pathmorphing,backgrounds,positioning,fit,shapes}
            
        \item In order to scale a picture containing node, option {\em scale}
            should be specified for both the tikzpicture environment and nodes
            within it.

        \item for code:

            \begin{lstlisting}[gobble=11]
            \path (fun3) .. controls +(left:4cm) and +(left:4cm) .. (fun1);
            \end{lstlisting}

            The first \verb=+(left:4cm)= is relative to \verb=(fun3)=, and the
            second \verb=+(left:4cm)= is relative to \verb=(fun1)=.

        \item For \verb=\newline= to have effect in the text of a node, option
            \verb=text width= should be used.

        \item to add a label under a given point within a path, write:
            \begin{lstlisting}[gobble=11]
            \path (B.south) -- +(-1, -0.5) coordinate[label=below:i] -- +(1, -0.5)
            coordinate[label=below:k] -- cycle;
            \end{lstlisting}

        \item \verb="|-"= and \verb="-|"= in command \verb=\path=. \verb="|-"=
            causes tikz to draw a vertical line first, and then a horizontal
            line, while \verb="-|"= acts in reverse order.

        \item Note that "below of=" is not the same as "below=of"
        
        \item Often times, you want to draw a line with its endpoints
            relative to other already-defined points. Suppose (A) and (B) are
            two already-defined points, and you want to draw a line with (A)+
            (1, 1) and (B)+(2, 3) as its endpoints, then you're possible to
            write code like:

            \begin{lstlisting}[gobble=11]
            \draw (A) +(1, 1) -- (B) +(2, 3);
            \end{lstlisting}

            Unfortunately, this doesn't work, instead, you should write code
            like:

            \begin{lstlisting}[gobble=11]
            \coordinate (a) at ($ (A) + (1, 1) $);
            \coordinate (b) at ($ (B) + (2, 3) $);
            \draw (a) -- (b);
            \end{lstlisting}

            For \verb=($ $)= to work, library \verb=calc= should be included.

        \item It's wired that text of nodes within a tree can't include
            command \verb=\verb=, but command \verb=\lstinline= is allowed.

        \item Consider the following code for a tree:

            \begin{lstlisting}[gobble=11]
            \node (vertical) [right=of origin, xshift=4cm] {\lstinline=NP^g=}
            [sibling distance=10mm]
                child {node {\lstinline=NN=}}
                child {node {\lstinline=CC=}}
            \end{lstlisting}

            Pay attention to the two option set for \verb=\node=, the first one,
            that is \verb![right=of origin, xshift=4cm]! is for \verb=\node=
            itself, and the second one, \verb![sibling distance=10mm]!, is for
            all the children that follows, and this option must be put before
            the children list.

        \item Suppose you want to add a math equation to the text of node as:

            \begin{lstlisting}[gobble=11]
            \node {
                \[
                    ...
                \]
            }
            \end{lstlisting}

            You'll find that this doesn't work, instead you should write like
            this:

            \begin{lstlisting}[gobble=11, escapechar=\%]
            \node [text width=%{\em width}%]{
                \[
                    ...
                \]
            }
            \end{lstlisting}

            This is not the first time I found that {\em text width} option is
            needed in order for adding some special text to node. I consider
            this as a bug of Tikz.

    \end{itemize}

\section{\LaTeX underline doesn't break across lines}

    To solve this problem, you can use package {\em ulem}, and use command
    \verb=\uline= provided by this package instead of \verb=\underline=.

    Beware of the default behavior of this package, which is to also underline
    those text in \verb={\em ...}= command, this can be avoided by loading
    package as:

    \begin{lstlisting}[gobble=3]
    \usepackage[normalem]{ulem}
    \end{lstlisting}

    Here are some other commands of this package:

    \begin{itemize}[topsep=0pt, itemsep=0pt]
        \item \verb=\uwave=: produce wavy underline
        \item \verb=\uuline=: double-underlined text
    \end{itemize}
    
\section{Misc}

    \begin{enumerate}[leftmargin=3ex, topsep=0pt]
        \item \verb=\setlength= command has effect only within current 
            environment.

        \item Commands that can produce horizontal line:
            \begin{enumerate}[topsep=0pt, leftmargin=4ex]
                \item \verb=\hrulefill=, this will produce a line of
                    \verb=\textwidth= long.

                \item \verb=\line(1,0){100}=, the \verb=100= is multiplied by
                    \verb=\unitlength=, you can use \verb=\setlength= to 
                    change its value.
            \end{enumerate}

        \item Commands used to create a box with background color: (require
            \verb=color= package)
            \begin{enumerate}[topsep=0pt, leftmargin=4ex]
                \item \verb=\colorbox col_spec {text}= like
                    \verb=\colorbox[gray]{0.8}{Hello, world}=

                \item \verb=\fcolorbox col_spec1 col_spec2 {text}=, like\\
                    \verb=\fcolorbox[gray]{0}{0.8}{Hello, world}=
            \end{enumerate}

            BTW, to create a color box of \verb=\textwidth= long, use command:

            \begin{lstlisting}[gobble=11]
            \colorbox[gray]{0.8}{\parbox{\textwidth}{Hello, world}}
            \end{lstlisting}

        \item It's very important to keep in mind that always add a blank line 
            when you mean to begin a new paragraph, it's easily to be forgot,
            since in some situations a new paragraph will be automatically
            started for you, for example, when adding a list environment
            immediately after a paragraph. But most of the time, this is not 
            the case.

        \item Sometimes it may be useful to create an anonymous environment 
            with \verb={}=. Suppose you want to temporarily change the value of
            \verb=\parskip= somewhere and don't want to affect its value
            elsewhere, surround that part of your article with \verb={}=

        \item To create an appendix with preceeding "Appendix":

            \begin{lstlisting}[gobble=11]
            \appendix
            \section*{Appendix}
            \end{lstlisting}

            Here I use the starred section command, since it will suppress the
            section number and will not increment the relevant counter and do
            not make an entry into the table of content.

        \item Don't use those two-letter font command like \verb=\bf=, they are
            obsolete, use \verb=\textbf{sth}= or \verb={\bfseries sth}= 
            instead.

        \item To input Chinese character, enclose those Chinese characters within
            CJK environment:

            \begin{lstlisting}[gobble=11, escapechar=\%]
            \begin{CJK}{%{\em encoding}%}{%{\em font}%}
                ...
            \end{CJK}
            \end{lstlisting}

            The most common set I use is:

            \begin{lstlisting}[gobble=11, escapechar=\%]
            \begin{CJK}{UTF8}{gkai}
                ...
            \end{CJK}
            \end{lstlisting}

        \item To make a Chinese title, you should surround both:

            \begin{lstlisting}[gobble=11]
            \title{...}
            \author{...}
            \date{...}
            \end{lstlisting}
            and
            \begin{lstlisting}[gobble=11]
            \maketitle
            \end{lstlisting}
            with
            \begin{lstlisting}[gobble=11]
            \begin{CJK}{...}{...}
            ...
            \end{CJK}
            \end{lstlisting}

        \item I found that {\em lstlisting} environment can't work with chinese
            character no matter what the encoding is. to make them work
            together, escapechar should be used, but even so, \verb=gobble=
            still cannot be used.

    \end{enumerate}

\end{document}
